/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.newmodel.LisDeviceset;
import static com.cxhd.lis.model.newmodel.LisDeviceset.lisDevicesetdao;
import com.cxhd.lis.model.newmodel.LisSampletype;
import com.google.gson.Gson;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;

/**
 *
 * @author cxhd
 */
@ControllerBind(controllerKey = "/view/lis/device")
public class LisDevicesetCtrl extends Controller {

    LisSampletype lisSam = new LisSampletype();
    Gson gson = new Gson();

    public void index() {
        render("/view/lis/yqwh.jsp");
    }

    public void getDeviceS() {// 仪器查询
//        renderJson(Db.use("mysql").find("select PK,DEVICEID,DEVICETYPE,USEDEPARTMENT,DEVICEMODEL,DEVICENAME,FACTORY,\n" +
//"(select t.DEVICETYPEDESC from lis_devicetype t where t.DEVICETYPE=s.DEVICETYPE ) DEVICETYPE1,\n" +
//"(select k.KSMC from lis_ksdm k WHERE k.KSDM=s.USEDEPARTMENT) USEDEPARTMENT1,DEVICESTATUS from lis_deviceset s LIMIT 0,10"));
        String sql = "select PK,NOTES,JUDGE,DEVICEID,DEVICETYPE,USEDEPARTMENT,DEVICETYPEPK,KSMCPK,DEVICEMODEL,DEVICENAME,FACTORY,(select t.DEVICETYPEDESC from lis_devicetype t where t.PK=s.DEVICETYPEPK ) DEVICETYPE1,\n"
                + "(select k.KSMC from lis_ksdm k WHERE k.PK=s.KSMCPK) USEDEPARTMENT1,DEVICESTATUS ,COMPORTN,BAUDRATES ";
        String param = " from lis_deviceset s where DEVICEMODEL LIKE '%" + getPara("searchContent") + "%' OR DEVICENAME LIKE '%" + getPara("searchContent") + "%'";
        Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage", getParaToInt("currentPage")), getParaToInt("pageSize", getParaToInt("pageSize")), sql, param);
        renderJson(userPage);
    }

    public void getDeviceRep() {
        List<Record> list = Db.use("mysql").find("select * from lis_deviceset where DEVICEMODEL=?", getPara("DEVICEMODEL"));
        if (list.size() != 0) {

            if (getPara("PK") != "" && getPara("PK") != null) {//判断是新增还是修改
                if (Integer.valueOf(getPara("PK")) == list.get(0).get("PK")) { //修改是允许修改（本身主键和查询到的主键相同）
                    renderText("false");
                } else {
                    renderText("true");
                }
            } else {
                renderText("true");
            }

        } else {
            renderText("false");
        }

    }

    public void deleteDeviceS() {
        lisDevicesetdao.deleteLisDevice(getPara("PK"));
        renderText("true");
    }

    public void saveDeviceS() {
        try {
            new LisDeviceset().saveNewLisDevice(getPara("DEVICEMODEL"), getPara("DEVICENAME"), getPara("FACTORY"), getPara("DEVICESTATUS"), getPara("PATH"), getPara("COMPORTN"), getPara("BAUDRATES"), getPara("DEVICETYPEPK"), getPara("KSMCPK"),getPara("NOTES"),getPara("JUDGE"));
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }

    }
     public void getSample() {
        renderJson(Db.use("mysql").find("select SAMPLEDESCRIBE from lis_sampletype"));
    }

    public void updateDeviceS() {
        lisDevicesetdao.updateLisDevice(getPara("PK"), getPara("DEVICEMODEL"), getPara("DEVICENAME"), getPara("FACTORY"), getPara("PATH"), getPara("DEVICESTATUS"), getPara("COMPORTN"), getPara("BAUDRATES"), getPara("DEVICETYPEPK"), getPara("KSMCPK"),getPara("NOTES"),getPara("JUDGE"));
        renderText("true");
    }

    public void getLisKs() {
        renderJson(Db.use("mysql").find("select PK, KSDM,KSMC FROM lis_ksdm"));
    }

    public void getLisDeviceType() {
        renderJson(Db.use("mysql").find("select PK,DEVICETYPE,DEVICETYPEDESC FROM lis_devicetype"));
    }

    public void getLisSam() {
        renderJson(lisSam.getLisSams());
    }
}
